package com.ch.engine.email;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.mail.MailProperties;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.mail.internet.MimeMessage;
import java.util.List;

/**
 * email工具类
 *
 * @author wangchunhui
 * @date 2024/8/22 16:11
 */
@Component
@Slf4j
public class EmailSendUtil {

    @Resource
    private JavaMailSender javaMailSender;
    @Resource
    private MailProperties mailProperties;

    /**
     * 简单邮件发送方法
     *
     * @param receiveEmail 邮箱地址
     * @param subject      邮件主题
     * @param content      邮件正文
     * @author wangchunhui
     * @date 2024/8/22 16:11
     */
    public void sendEmail(String receiveEmail, String subject, String content) {
        // 创建简单邮件消息
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(mailProperties.getUsername());
        message.setTo(receiveEmail);
        message.setSubject(subject);
        message.setText(content);
        try {
            javaMailSender.send(message);
        } catch (Exception e) {
            log.error("邮件发送失败！", e);
        }
    }

    /**
     * 发送邮箱包含附件
     *
     * @param receiveEmails 接收人邮箱
     * @param copyEmails    抄送邮箱
     * @param subject       主体
     * @param content       正文
     * @param isHtml        是否html
     * @param attachMails   附件
     * @author wangchunhui
     * @date 2024/8/22 16:11
     */
    public void sendAttachmentEmail(String[] receiveEmails, String[] copyEmails, String subject, String content, boolean isHtml, List<AttachMail> attachMails) {
        try {
            //创建多部分邮件信息
            MimeMessage mimeMessage = javaMailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
            helper.setFrom(mailProperties.getUsername());
            helper.setTo(receiveEmails);
            helper.setCc(copyEmails);
            helper.setSubject(subject);
            helper.setText(content, isHtml);
            //添加附件
            for (AttachMail attachMail : attachMails) {
                helper.addAttachment(attachMail.getFileName(), attachMail.getInputStreamSource());
            }
            javaMailSender.send(mimeMessage);
        } catch (Exception e) {
            log.error("邮件发送失败！", e);
        }
    }

//    /**
//     * excel字节数组转换html字符串
//     *
//     * @param sourceExcel excel字节数组
//     * @return java.lang.String
//     * @author wangchunhui
//     * @date 2024/8/22 16:12
//     */
//    public static String convertExcelToHtml(byte[] sourceExcel) {
//        if (Objects.isNull(sourceExcel)) {
//            return "";
//        }
//        ByteArrayInputStream inputStream = new ByteArrayInputStream(sourceExcel);
//        Workbook workbook = new Workbook();
//        workbook.loadFromStream(inputStream);
//        Worksheet worksheet = workbook.getWorksheets().get(0);
//        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
//        worksheet.saveToHtml(byteArrayOutputStream, HTMLOptions.Default);
//        return byteArrayOutputStream.toString();
//    }
}
